package bookticket.crm.recharge.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import bookticket.crm.recharge.entity.RechargeEntity;
import bookticket.crm.recharge.service.RechargeService;
import bookticket.crm.user.entity.UserEntity;

@Controller
@RequestMapping("/recharge")
public class RechargeController {

	@Autowired
	private RechargeService service;
	
	@ResponseBody
	@RequestMapping("/add")
	public Map<String,String> add(RechargeEntity entity,UserEntity userEntity,@RequestParam("phone")String phone,@RequestParam("rechargeAmount")Integer rechargeAmount){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String createDate = sdf.format(date);
		entity.setRechargeDate(createDate);
		//取得当前用户充值前的余额
		Integer old = service.findUserByPhone(phone).getResidualAmount();
		//充值后的余额
		Integer newResidualAmount = old+rechargeAmount;
		userEntity.setResidualAmount(newResidualAmount);
		int userCounter = service.updateUserByPhone(userEntity);
		Map<String,String> map = new HashMap<String,String>();
		int counter = service.add(entity);
		if(counter == 1 && userCounter==1){
			map.put("msg", "充值成功");
			map.put("result","success" );
		}
		else{
			map.put("msg", "充值失败");
			map.put("result","failure" );
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/findAll")
	public Map<String,Object> findAll(Integer page,Integer rows,HttpServletRequest request){
//		HttpSession session = request.getSession();
//		String phone = (String) session.getAttribute("phone");
		
		String phone = (String) request.getSession().getAttribute("phone");
	//	System.err.println(phone);
		
		
		Integer before = (page-1)*rows;
		Integer after = rows;
		
		Map<String, Object> pageMap = new HashMap<String,Object>();
		pageMap.put("before", before);
		pageMap.put("after", after);
		pageMap.put("phone", phone);
		
		List<RechargeEntity> list = service.findAll(pageMap);
		for (RechargeEntity rechargeEntity : list) {
			
			System.err.println(rechargeEntity);
		}
		Map<String, Object> map = new HashMap<String,Object>();
		
		//分页查询中固定的写法  total rows
		int total = service.getTotalCounter(phone);
		map.put("total", total);
		map.put("rows", list);
		map.put("result", "success");
		
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/findUserTotalRecharge")
	public int findUserTotalRecharge(){
		
		return service.findUserTotalRecharge();
	}
	
	@ResponseBody
	@RequestMapping("/show")
	public void show(@RequestParam("rechargeAmount") Integer rechargeAmount,@RequestParam("phone") String phone){
		
		System.err.println(rechargeAmount+":"+phone);
	}
}
